const app = getApp();
var QRCode = require('../../utils/weapp-qrcode.js')

var qrcode;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    qrcodeInfo: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    qrcodeInfo: '',
    qrcodewidth: 0.6 * app.globalData.windowWidth,
    qrcodeheight: 0.6 * app.globalData.windowWidth
  },
  ready: function () {
    qrcode = new QRCode('canvas', {
      usingIn: this,
      text: this.data.qrcodeInfo,
      // image: '',
      width: 0.6 * app.globalData.windowWidth,
      height: 0.6 * app.globalData.windowWidth,
      colorDark: "#003894",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },

  /**
   * 组件的方法列表
   */
  methods: {
    confirmHandler: function (e) {
      var value = e.detail.value
      qrcode.makeCode(value)
    },
    inputHandler: function (e) {
      var value = e.detail.value
      this.setData({
        text: value
      })
    },
    // tapHandler: function () {
    //   console.log('tap')
    //   // 传入字符串生成qrcode
    //   qrcode.makeCode(this.data.text)
    // },
    save: function () {
      console.log('save')
      wx.showActionSheet({
        itemList: ['保存图片'],
        success: function (res) {
          console.log(res.tapIndex)
          if (res.tapIndex == 0) {
            qrcode.exportImage(function (path) {
              wx.saveImageToPhotosAlbum({
                filePath: path,
              })
            })
          }
        }
      })
    }
  }
})
